package com.chenfan.mcn.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.common.extension.dict.DictStream;
import com.chenfan.common.extension.dict.DictTranslationUtils;
import com.chenfan.mcn.clients.baseinfo.BaseInfoClient;
import com.chenfan.mcn.clients.baseinfo.DescByCodeDTO;
import com.chenfan.mcn.constant.DictTypeCst;
import com.chenfan.mcn.constant.NumberCst;
import com.chenfan.mcn.dao.StarAccountInfoMapper;
import com.chenfan.mcn.dao.StarBasicDataMapper;
import com.chenfan.mcn.dao.StarBillingHeaderMapper;
import com.chenfan.mcn.model.McnPage;
import com.chenfan.mcn.model.StarAccountInfo;
import com.chenfan.mcn.model.StarBillingHeader;
import com.chenfan.mcn.model.vo.StarAccountOrBillingInfoResp;
import com.chenfan.mcn.service.StarAccountInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author liuyang
 * @Date 2021/1/7
 * @Time 20:34
 * @Version V1.0
 **/
@Service
public class StarAccountInfoServiceImpl extends ServiceImpl<StarAccountInfoMapper, StarAccountInfo> implements StarAccountInfoService {

    @Autowired
    private BaseInfoClient baseInfoClient;
    @Resource
    private StarBillingHeaderMapper starBillingHeaderMapper;
    @Resource
    private StarBasicDataMapper starBasicDataMapper;
    @Resource
    private StarAccountInfoMapper starAccountInfoMapper;

    @Override
    public StarAccountInfo getByStarId(Long starId) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(StarAccountInfo.class).eq(StarAccountInfo::getStarId, starId));
    }

    @Override
    public Page<StarAccountInfo> searchAccount(McnPage<StarAccountInfo> mcnPage, String accountName, String accountNumber, Long starId,
                                               boolean filterInvalid) {
        LambdaQueryWrapper<StarAccountInfo> starAccountInfoLambdaQueryWrapper = Wrappers.lambdaQuery(StarAccountInfo.class)
                .eq(StarAccountInfo::getIsDelete, Boolean.FALSE)
                .eq(StarAccountInfo::getStarId, starId)
                .ne(filterInvalid, StarAccountInfo::getAccountType, "invalid_account_number")
                .like(StringUtils.isNotBlank(accountName), StarAccountInfo::getAccountName, accountName)
                .like(StringUtils.isNotBlank(accountNumber), StarAccountInfo::getAccountNumber, accountNumber)
                .orderByDesc(StarAccountInfo::getCreateDate);
        McnPage<StarAccountInfo> accountPage = baseMapper.selectPage(mcnPage, starAccountInfoLambdaQueryWrapper);
        List<StarAccountInfo> accountList = accountPage.getRecords();
        if (CollectionUtil.isNotEmpty(accountList)) {
            //字典翻译
            DictTranslationUtils.translation(() -> DictStream.of(DictStream.map(DictTypeCst.ACCOUNT_TYPE, StarAccountInfo::getAccountType, StarAccountInfo::setAccountType)
                    ),(dictProfileList, dictList) -> baseInfoClient.getDescByCode(new DescByCodeDTO().setDictsCodes(dictList).setProfileCodes(dictProfileList)).getObj(),
                    accountList
            );
        }
        return accountPage;
    }

    @Override
    public StarAccountInfo getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<StarAccountInfo> getListByStarId(Long starId) {
        return baseMapper.selectList(Wrappers.lambdaQuery(StarAccountInfo.class).eq(StarAccountInfo::getStarId, starId)
                .eq(StarAccountInfo::getIsDelete, NumberCst.ZERO).eq(StarAccountInfo::getIsDelete, NumberCst.ZERO));
    }

    @Override
    public StarAccountInfo getByName(String starName) {
        return baseMapper.selectOne(Wrappers.lambdaQuery(StarAccountInfo.class)
                .eq(StarAccountInfo::getAccountName, starName));
    }

    @Override
    public List<StarAccountOrBillingInfoResp> allAccountInfo(Integer status) {
        //获取所有红人信息
        List<StarAccountOrBillingInfoResp> allStar = starBasicDataMapper.allStar(null);
        List<StarBillingHeader> billingHeaders = starBillingHeaderMapper.selectList(Wrappers.lambdaQuery(StarBillingHeader.class)
                .eq(Objects.nonNull(status), StarBillingHeader::getIsEnable, status)
                .eq(StarBillingHeader::getIsDelete, NumberCst.ZERO) );
        Map<Long, List<StarBillingHeader>> billingMap = new HashMap<>(16);
        if (CollectionUtil.isNotEmpty(billingHeaders)) {
            billingMap = billingHeaders.stream().collect(Collectors.groupingBy(StarBillingHeader::getStarId));
        }
        List<StarAccountInfo> accountInfos = starAccountInfoMapper.selectList(Wrappers.lambdaQuery(StarAccountInfo.class)
                .eq(StarAccountInfo::getIsDelete, NumberCst.ZERO) );
        Map<Long, List<StarAccountInfo>> accountMap = new HashMap<>(16);
        if (CollectionUtil.isNotEmpty(accountInfos)) {
            accountMap = accountInfos.stream().collect(Collectors.groupingBy(StarAccountInfo::getStarId));
        }
        if (CollectionUtil.isNotEmpty(allStar)) {
            for (StarAccountOrBillingInfoResp resp : allStar) {
                resp.setAccountInfos(accountMap.get(resp.getStarId()));
                resp.setBillingHeaders(billingMap.get(resp.getStarId()));
            }
        }
        return allStar;
    }
}
